#!/bin/bash
#  判断nginx能否正常访问
curl http://127.0.0.1 &> /dev/null
if [ `echo $?` != 0 ];then    #  如果不能访问，就启动nginx
    /usr/local/nginx/sbin/nginx
fi

# 恢复默认配置文件
\cp  /usr/local/nginx/conf/nginx.conf.default \
/usr/local/nginx/conf/nginx.conf

#  为nginx增加网站认证功能
sed -ir '40a auth_basic "password";\
auth_basic_user_file "/usr/local/nginx/pass";' \
    /usr/local/nginx/conf/nginx.conf
#  重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload
cd /usr/local/nginx
yum -y install httpd-tools &> /dev/null \
   && \
read -p '请输入登陆用户名：' user  # 创建登陆用户
htpasswd -c pass $user  # 之后会输入两次密码
echo -e "\033[36m===============================================\033[0m"
echo -e "\033[5;36m网站认证功能开启成功！\033[0m"
